import java.util.*;

/**
 * @author gao
 * @date 2025/6/4 18:31
 */

public class Main {
    static class IDcard {
        String id;
        String date;

        public IDcard(String id, String date) {
            this.id = id;
            this.date = date;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = Integer.parseInt(in.nextLine());
        List<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(in.nextLine());
        }

        while (in.hasNextLine()) {
            String command = in.nextLine();

            if (command.equals("sort1")) {
                processSort1(list);
            } else if (command.equals("sort2")) {
                processSort2(list);
            } else {
                System.out.println("exit");
            }
        }
    }

    private static String extractDate(String idCard) {
        String year = idCard.substring(6,10);
        String month = idCard.substring(10,12);
        String day = idCard.substring(12,14);

        return year + "-" + month + "-" + day;
    }

    private static void processSort1(List<String> idCards) {
        List<String> list = new ArrayList<>();

        for (String i : idCards) {
            list.add(extractDate(i));
        }

        Collections.sort(list);

        for (String i : list) {
            System.out.println(i);
        }
    }

    private static void processSort2(List<String> idCards) {
        List<IDcard> list = new ArrayList<>();
        for (String i : idCards) {
            list.add(new IDcard(i,extractDate(i)));
        }
        Collections.sort(list, new Comparator<IDcard>() {
            @Override
            public int compare(IDcard o1, IDcard o2) {
                return o1.date.compareTo(o2.date);
            }
        });
        for (IDcard i : list) {
            System.out.println(i.id);
        }
    }
}